package com.xxxx.yygh.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxxx.yygh.common.result.Result;
import com.xxxx.yygh.common.utils.Md5Util;
import com.xxxx.yygh.hospital.service.HospitalSetService;
import com.xxxx.yygh.model.hosp.HospitalSet;
import com.xxxx.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @author Lee.s
 * @description
 * @date 2021/03/11 9:19
 */
@Api(tags ="医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@CrossOrigin
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    //1 查询医院设置表所有信息
    @ApiOperation(value = "获取所有医院设置信息")
    @GetMapping("/findAll")
    public Result findAllHospitalSet() {
        //调用service的方法
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "根据id删除医院信息(逻辑删除)")
    @DeleteMapping("/{id}")
    public Result removeHospitalSet(@PathVariable Long id) {
        boolean flag = hospitalSetService.removeById(id);
        if (flag) {
            return Result.ok();
        }
        return Result.fail();
    }

    // 3.条件查询带分页
    @ApiOperation("条件查询带分页")
    @PostMapping("findHospitalSet/{current}/{limit}")
    public Result findHospitalSet(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        String hosName =hospitalSetQueryVo.getHosname();
        String hosCode = hospitalSetQueryVo.getHoscode();
        LambdaQueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<HospitalSet>().lambda();
        if (StringUtils.isNotBlank(hosName)) {
            queryWrapper.like(HospitalSet::getHosname, hosName);
        }
        if (StringUtils.isNotBlank(hosCode)) {
            queryWrapper.eq(HospitalSet::hashCode, hosCode);
        }
        Page<HospitalSet> page = new Page<>(current, limit);
        Page<HospitalSet> result = hospitalSetService.page(page, queryWrapper);
        return Result.ok(result);
    }

    // 4.添加医院设置
    @ApiOperation("添加医院设置")
    @PostMapping("/saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        //设置状态 1 使用 0 不能使用
        hospitalSet.setStatus(1);

        Random random = new Random();
        hospitalSet.setSignKey(Md5Util.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        boolean flag = hospitalSetService.save(hospitalSet);

        if (!flag) {
            return Result.fail();
        }
        return Result.ok();
    }

    // 5.根据id获取医院设置
    @ApiOperation("根据id获取医院设置")
    @GetMapping("/getHospitalSet/{id}")
    public Result getHospitalSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    // 6.修改医院设置
    @ApiOperation("修改医院设置")
    @PostMapping("/updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        boolean flag = hospitalSetService.updateById(hospitalSet);

        if (!flag) {
            return Result.fail();
        }
        return Result.ok();
    }

    // 7.批量删除医院设置
    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping("/batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList) {
        boolean flag = hospitalSetService.removeByIds(idList);
        if (flag) {
            return Result.ok();
        }
        return Result.fail();
    }

    // 8.医院锁定和解锁
    @ApiOperation("医院锁定和解锁")
    @PutMapping("/lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status) {
        //根据id查询医院设置信息
        HospitalSet hospitalSet = new HospitalSet();
        hospitalSet.setId(id);
        //设置状态
        hospitalSet.setStatus(status);
        //调用方法
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag) {
            return Result.ok();
        }
        return Result.fail();
    }

    // 9.发送签名秘钥
    @ApiOperation("发送签名秘钥")
    @PutMapping("sendKey/{id}")
    public Result lockHospitalSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODO 发送短信

        return Result.ok();
    }

}
